<%@page import="nl.hva.web.mvcwebapp.models.News"%>
<%@include file="/WEB-INF/templates/configuration.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
    HttpSession session_user = request.getSession(true);
    String firstname = (String) session_user.getAttribute("user_firstname");
    int user_id = 0;
    int user_type = 0;
    if (firstname != null) {
        user_id = Integer.valueOf("" + session_user.getAttribute("user_id"));
        user_type = Integer.valueOf("" + session_user.getAttribute("user_type"));
    }
if(user_type ==3){
%>
<h2>Users</h2>
<c:choose>
    <c:when test="${aantalGebruikers != 0}">
        <!-- Wanneer er gebruikers opgeslagen zijn, worden ze hier getoond -->
        <table class="zebra">
            <tr>
                <td>
                    <strong>First Name</strong>
                </td>
                <td>
                    <strong>Last Name</strong>
                </td>
                <td>
                    <strong>E-mail</strong>
                </td>
                <td></td>
            </tr>
            <c:forEach var="tempGebruiker" items="${gebruikersUitSessie}">
                <!-- Per gebruiker wordt nu een rij aangemaakt met daarin zijn gegevens -->
                <input type="hidden" name="id" id="id" value="${tempGebruiker.userId}"></input>
                <tr>
                    <td>
                        ${tempGebruiker.userFirstName}
                    </td>
                    <td>
                        ${tempGebruiker.userLastName}
                    </td>
                    <td>
                        ${tempGebruiker.userEmail}
                    </td>
                    <td>
                        <a href="${url}user/user_profile?id=${tempGebruiker.userId}" class="icon view">view</a> 
                        <a href="${url}user/edit?id=${tempGebruiker.userId}" class="icon edit">edit</a> 
                        <a href="javascript:if(confirm('Weet u het zeker dat u deze gebruiker wil verwijderen?'))
                           window.location='${url}user/delete?id=${tempGebruiker.userId}';" class="icon delete">delete</a>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </c:when>
    <c:otherwise>
        <!-- Als er geen gebruikers zijn, wordt deze melding getoond -->
        There are no users found.
    </c:otherwise>
</c:choose>
<p>
    <a href="${url}user/new">Add a new user</a>
</p>






<br>
<br>
<br>
<br>

<h2>Courses</h2>
<c:choose>
    <c:when test="${amountcourses != 0}">
        <!-- Wanneer er gebruikers opgeslagen zijn, worden ze hier getoond -->
        <table class="zebra">
            <thead>
                <tr>
                    <th style="width:25%;"> 
                        <strong>Name</strong>
                    </th>
                    <th style="width:50%;">
                        <strong>Description</strong>
                    </th>
                    <th style="width:15%;">
                        <strong>Controls</strong>
                    </th>
                </tr>
            </thead>
            <c:forEach var="tempCourse" items="${courses}">
                <!-- Per gebruiker wordt nu een rij aangemaakt met daarin zijn gegevens -->
                <tr>
                    <td>
                        <a href="${url}course/view?id=${tempCourse.courseid}">${tempCourse.coursetitle}</a>
                    </td>
                    <td>
                        ${tempCourse.coursedescription}
                    </td>
                    <td>
                        <a class="icon view" href="${url}course/view?id=${tempCourse.courseid}">view</a>
                        <a class="icon edit" href="${url}course/edit?id=${tempCourse.courseid}">edit</a>
                        <a class="icon delete" href="javascript:if(confirm('Weet u zeker dat u deze cursus wil verwijderen?'))
                           window.location='${url}course/delete?courseid=${tempCourse.courseid}';">delete</a>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </c:when>
    <c:otherwise>
        <!-- Als er geen cursussen zijn, wordt deze melding getoond -->
        There are no courses found.
    </c:otherwise>
</c:choose>

<%if (user_type == 2 || user_type == 3) {
%>
<p>
    <a class="button" href="${url}course/new">Add a new course</a>
</p>

<%                }%>

<br>
<br>
<br>

<h2>News items</h2>
<c:choose>
    <c:when test="${aantalNieuws != 0}">
        <!-- Wanneer er gebruikers opgeslagen zijn, worden ze hier getoond -->
        <table class="zebra">
            <tr>
                <td>
                    <strong>News Title</strong>
                </td>
                <td>
                    <strong>News Content</strong>
                </td>
                <td>
                    <strong>News Date</strong>
                </td>
                <td></td>
            </tr>
            <c:forEach var="tempNieuws" items="${nieuwsUitSessie}">
                <!-- Per gebruiker wordt nu een rij aangemaakt met daarin zijn gegevens -->
                <tr>
                    <td>
                        ${tempNieuws.newstitle}
                    </td>
<%
                            String url = (String)pageContext.getAttribute("url");
                            News temp_news = (News) pageContext.getAttribute("tempNieuws");
                            String temp_content = temp_news.getNewscontent();
                            int temp_news_id = temp_news.getNewsid();
                            if(temp_content.length()>= 600){
                            temp_content = temp_content.substring(0, 600);
                            temp_content +="...<br/><a href="+url+"news/view?newsid="+temp_news_id+">Continue reading..</a>";
                                                       }else{
                                
                                                       }
                            %>
                            <td class="news-item-content">
                                <%=temp_content%>
                            </td>
                    <td>
                        ${tempNieuws.newsdate}
                    </td>
                    <td>
                        <a href="${url}news/edit?newsid=${tempNieuws.newsid}" class="icon view">edit</a> 
                        <a href="${url}news/view?newsid=${tempNieuws.newsid}" class="icon edit">view</a> 
                        <a href="${url}news/delete?newsid=${tempNieuws.newsid}" class="icon delete">delete</a>
                        <!--                                <a href="javascript:if(confirm('Weet u zeker dat u dit nieuws wil verwijderen?'))
                                                           window.location='news/delete?newsid=${tempNieuws.newsid}';">delete</a>-->
                    </td>
                </tr>
            </c:forEach>
        </table>
    </c:when>
    <c:otherwise>
        <!-- Als er geen gebruikers zijn, wordt deze melding getoond -->
        There is no news found.
    </c:otherwise>
</c:choose>
<%if (user_type == 2 || user_type == 3) {
%>
<p>
    <!--News course overview-->       
<!--    <a href="${url}news/view/course?courseid=1">view</a>-->

    <a class="button" href="${url}news/new">Add a new News item</a>
</p>

<%            }%>

<br>
<br>
<br>

<h2>Forum</h2>
<c:choose>
    <c:when test="${aantalForums != 0 || aantalThreads != 0}">
        <!-- Wanneer er forums opgeslagen zijn, worden ze hier getoond -->
        <table class="zebra">
            <tr>
                <th>
                    <strong>Forum topics</strong>
                </th>
                <th>
                    <strong>Description</strong>
                </th>
                <th>
                    <strong>Author</strong>
                </th>
                <th>

                </th>
            </tr>
            <c:forEach var="tempForum" items="${forumsUitSessie}">
                <!-- Forum topics --------------------------------------------------->
                <c:choose>
                    <c:when test="${tempForum.forumtopic_isdeleted == 0}">
                        <tr>
                            <td width="120px;">
                                <a href="${url}forum/topic_view?topic_id=${tempForum.forumtopicid}">${tempForum.forumTopicName}</a>
                            </td>
                            <td>
                                ${tempForum.forumtopicbody}
                            </td>
                            <td>
                                ${tempForum.user.userName}
                            </td>
                            <td>
                                <a class="icon view" href="${url}forum/topic_view?topic_id=${tempForum.forumtopicid}">${tempForum.forumTopicName}</a>
                                <c:choose>
                                    <c:when test="${tempForum.user.userId == currentUserId}">
                                        <a class="icon delete" title="Delete topic" href="${url}forum/delete_topic?topic_id=${tempForum.forumtopicid}"></a>

                                    </c:when>
                                    <c:when test="${currentUserType == 3}">
                                        <a class="icon delete" title="Admin delete topic" href="${url}forum/admin_delete_topic?topic_id=${tempForum.forumtopicid}"></a>
                                    </c:when>

                                </c:choose>


                            </td>
                        </tr>
                    </c:when>

                </c:choose>
            </c:forEach>

            <tr height="10px;"></tr>
        </table>
        <table class="zebra">

            <!-- Forum threads ----------------------------------------------------->
            <tr>
                <th>
                    <strong>Forum threads</strong>
                </th>
                <th>
                    <strong>Description</strong>
                </th>
                <th>
                    <strong>Author</strong>
                </th>
                <th>
                    <strong> </strong>
                </th>
            </tr>
            <c:forEach var="tempThread" items="${threadsUitSessie}">
                <c:choose>
                    <c:when test="${tempThread.forumthread_isdeleted == 0}">

                        <tr>
                            <td width="120px;">
                                <a href="${url}forum/thread_view?thread_id=${tempThread.forumThreadId}">${tempThread.forumThreadName}</a>
                            </td>
                            <td>
                                ${tempThread.forumThreadBody}
                            </td>
                            <td>
                                ${tempThread.user.userName}
                            </td>
                            <td>
                                <a class="icon view" href="${url}forum/thread_view?thread_id=${tempThread.forumThreadId}">${tempThread.forumThreadName}</a>
                                <c:choose>
                                    <c:when test="${tempThread.user.userId == currentUserId}">
                                        <a class="icon delete" title="Delete thread" href="${url}forum/delete_thread?thread_id=${tempThread.forumThreadId}"></a>

                                    </c:when>
                                    <c:when test="${currentUserType == 3}">
                                        <a class="icon delete" title="Admin delete thread" href="${url}forum/admin_delete_thread?thread_id=${tempThread.forumThreadId}"></a>
                                    </c:when>

                                </c:choose>

                            </td>
                        </tr>
                    </c:when>

                </c:choose>
            </c:forEach>
        </table>
    </c:when>
    <c:otherwise>
        <!-- Als er geen gebruikers zijn, wordt deze melding getoond -->
        There are no forums or threads found.
    </c:otherwise>
</c:choose>

</br>
<p>
    <a class="button" href="${url}forum/topic_add">Start new topic</a>
</p>
<p>
    <a class="button" href="${url}forum/thread_add">Start new thread</a>
</p>
<%}else{%>

<p>Geen toegang</p>
<%}%>